﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace HelixSoftware.Azure.Data
{
    [Serializable()]
    public abstract class DataObject : INotifyPropertyChanged, ISupportInitialize
    {
        #region Properties

        private bool _IsInitializing;

        private bool _IsNew;
        public bool IsNew
        {
            get { return _IsNew; }
        }

        private bool _IsChanged;
        public virtual bool IsChanged
        {
            get { return _IsChanged; }
        }

        private bool _DeleteOnSave;
        public bool DeleteOnSave
        {
            get { return _DeleteOnSave; }
            set { _DeleteOnSave = value; }
        }

        public SaveAction SaveAction
        {
            get
            {
                if (this.IsNew)
                {
                    if (!this.DeleteOnSave)
                    {
                        return SaveAction.Insert;
                    }
                }
                else if (this.DeleteOnSave)
                {
                    return SaveAction.Delete;
                }
                else if (this.IsChanged)
                {
                    return SaveAction.Update;
                }

                return SaveAction.None;
            }
        }

        private IDictionary<String, Object> _Values;
        private IDictionary<String, Object> Values
        {
            get
            {
                if (_Values == null)
                {
                    _Values = new Dictionary<String, Object>();
                }

                return _Values;
            }
        }

        protected T GetValue<T>(PropertyMetadata<T> propertyMetadata)
        {
            if (this.Values.ContainsKey(propertyMetadata.Name))
            {
                return (T)this.Values[propertyMetadata.Name];
            }
            else
            {
                return propertyMetadata.DefaultValue;
            }
        }

        protected void SetValue<T>(PropertyMetadata<T> propertyMetadata, T value)
        {
            var currentValue = this.GetValue(propertyMetadata);
            if (!object.Equals(currentValue, value))
            {
                this.Values[propertyMetadata.Name] = value;
                this.OnPropertyChanged(propertyMetadata.Name);
            }
        }

        #endregion

        #region Events

        [field: NonSerialized()]
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (!_IsInitializing)
            {
                if (!_IsChanged)
                {
                    _IsChanged = true;
                }

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

        #endregion

        #region Constructors

        protected DataObject()
        {
            this.SetIsNew(true);
        }

        #endregion

        #region Methods

        protected void SetIsNew(bool value)
        {
            _IsNew = value;
        }

        public void BeginInit()
        {
            _IsInitializing = true;
        }

        public void EndInit()
        {
            _IsInitializing = false;
        }

        #endregion
    }
}
